package main

import (
	"fmt"
)

type myInt uint32
type myStr = string

// Stu 自定义复合类型
type Stu struct {
	name  string
	age   int
	hobby []string
}

func f1(stu *Stu) {
	// (*stu).age = 999
	stu.age = 999 // 语法糖
}

func main() {
	var num1 myInt
	num1 = 199
	fmt.Printf("mun1=%d (%T)\n", num1, num1) // mun1=199 (main.myInt)
	var s1 myStr
	s1 = "aaaaa"
	fmt.Printf("s1=%s(%T)\n", s1, s1)                         // s1=aaaaa(string)
	fmt.Printf("rune(%T) int32(%T)\n", new(rune), new(int32)) // rune(*int32) int32(*int32)

	var stu1 Stu
	stu1.age = 11
	stu1.name = "hhhhh"
	stu1.hobby = []string{"aaa", "bbbb"}
	fmt.Println(stu1)
	var stu2 = stu1
	stu2.age = 99
	fmt.Println(stu1, stu2) // {hhhhh 11 [aaa bbbb]} {hhhhh 99 [aaa bbbb]}
	// 修改结构体内部数据
	f1(&stu1)
	fmt.Println(stu1) // {hhhhh 999 [aaa bbbb]}

	// 匿名结构体
	var p1 struct {
		y, x int
	}
	p1.x = 99
	p1.y = 16
	fmt.Printf("p1=%v(%T)\n", p1, p1) // p1={16 99}(struct { y int; x int })

	// 指针类型结构体，赋予结构体引用传递的能力
	var stu3 = new(Stu)
	(*stu3).age = 11 // 可简写为：stu3.age = 11
	var stu4 = stu3
	stu4.name = "hello"
	fmt.Println(stu3, stu4) // &{hello 11 []} &{hello 11 []}

	// 初始化
	var stu5 = Stu{
		name:  "hehe",
		age:   15,
		hobby: []string{"asaaa", "bbbbb"},
	}
	fmt.Println(stu5)                               // {hehe 15 [asaaa bbbbb]}
	var stu6 = Stu{"aaaa", 34, []string{"asdadsf"}} // 值的顺序必须与结构体定义顺序一样
	fmt.Println(stu6)                               // {aaaa 34 [asdadsf]}

}
